package com.yonder.service.impl;

import com.yonder.dao.OrderSettingDao;
import com.yonder.pojo.OrderSetting;
import com.yonder.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName OrderSettingServiceImpl
 * @Description TODO
 * @Author zxs
 * @Date 2021/3/22 10:42
 * Version 1.0
 * <p>
 * <p>
 * <p>
 * 这里是处理批量预约功能的service
 */

@Service
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<String[]> list) {
        /*
         * 因为POI工具类实现了把excel中的数据以每行一个数组形式存放在list集合中，所有需要遍历出来给OrderSetting实体类
         * 赋值
         * */
        Integer count = 0;
        if (list != null && list.size() > 0) {
            for (String[] str : list) {
                for (int i = 0; i < str.length; i++) {
                    OrderSetting orderSetting = new OrderSetting();
//                    System.out.println("str[0]=="+str[0]);
                    orderSetting.setOrderDate(new Date(str[0]));//预约日期
                    orderSetting.setNumber(Integer.parseInt(str[1]));//预约人数
                    orderSetting.setReservations(0);   //已经预约人数
                    count = 0;
                    count = orderSettingDao.findByDate(orderSetting.getOrderDate());
//                    System.out.println("di"+i+"次的count=="+count);
                    if (count > 0) {
                        //表示 t_ordersetting预约表里已经有数据了，需要更新数据
                        orderSettingDao.updateByOrderSetting(orderSetting);
                    } else {
                        orderSettingDao.add(orderSetting);
                    }
                }
            }
        }
    }
}
